/**
 * Created by leo on 5/30/16.
 */

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// Device Schema
var deviceSchema = new Schema({
    sid: {
        type: String,
        rquire: true
    },
    name: {
        type: String,
        require: true
    },
    image_url: {
        type: String
    },
    status: {
        type: Boolean,
        require: true
    },
    category: {
        type: Schema.Types.ObjectId,
        ref: 'Category'
    },
    room: {
        type: Schema.Types.ObjectId,
        ref: 'Room'
    },
    intro: {
        type: String
    },
    isFavorite: {
        type: Boolean,
        require: true
    }
}, {
    collection: 'device',
    versionKey: false
});

var Device = module.exports = mongoose.model('Device', deviceSchema);

exports.Device = mongoose.model('Device', deviceSchema);

// Get Devices
module.exports.getDevices = function(callback, limit) {
    Device.find(callback).limit(limit);
}

// Get Device By Id
module.exports.getDeviceById = function (id, callback) {
    Device.findById(id, callback);
}

// Add Device
module.exports.addDevice = function (device, callback) {
    Device.create(device, callback);
}

// Update Device
module.exports.updateDevice = function (id, device, options, callback) {
    var query = {_id: id};
    //var update = {
    //    name: device.name,
    //    image_url: device.image_url,
    //    status: device.status,
    //    category: device.category,
    //    room: device.room,
    //    intro: device.intro,
    //    isFavorite: device.isFavorite
    //}
    Device.findOneAndUpdate(query, device, options, callback);
}

//// Update Device
//module.exports.updateDevice = function (id, device, options, callback) {
//    var query = {_id: id};
//    var update = {
//        name: device.name,
//        image_url: device.image_url,
//        status: device.status,
//        category: device.category,
//        room: device.room,
//        intro: device.intro,
//        isFavorite: device.isFavorite
//    }
//    Device.findOneAndUpdate(query, update, options, callback);
//}

// Delete Device
module.exports.removeDevice = function (id, callback) {
    var query = {_id: id};
    Device.remove(query, callback);
}

